/**
 * 
 */
package com.thon.dao.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.thon.dao.user.UserDao;
import com.thon.dao.util.AbstractDao;
import com.thon.dao.util.Page;
import com.thon.entity.user.User;

/**
 * @author manpiaoyi
 * @email manpiaoyi@126.com
 * @date 2014-10-14
 * @description:
 */
@Repository
public class UserDaoImpl extends AbstractDao<User> implements UserDao {

	public UserDaoImpl() {
		super(User.class);
	}

	@Override
	public User findByLoginName(String loginName, String status) {
		String queryString = "FROM User u where u.loginName = ? and u.status = ?";

		if (status == null) {
			queryString = "FROM User u where u.loginName = ? ";
			return findOne(queryString, "query.UserByLoginName", loginName);
		}

		return findOne(queryString, "query.UserByLoginNameActive", loginName,
				status);
	}

	public User findByEmail(String email) {
		String queryString = "from User where loginName=?";
		return findOne(queryString, null, email);
	}

	public User findByName(String name){
		String queryString = "from User where name=? ";
		return findOne(queryString, null, name);
	}
	
	public User findByName(String name, String status){		
		
		String queryString = "FROM User u where u.name = ? and u.status = ? ";

		if (status == null) {
			queryString = "FROM User u where u.name = ? ";
			return findOne(queryString, "query.UserByLoginName", name);
		}

		return findOne(queryString, "query.UserByLoginNameActive", name,
				status);
	}
	
	@Override
	public List<User> listAllUsers() {
		String queryString = "FROM User ORDER BY lastLoginDate Desc";
		return findList(queryString, null);
	}

	@Override
	public Page<User> findUserPage(Page<User> page, String role) {
		String queryString = "FROM User u WHERE u.roles like ? ORDER BY u.lastLoginDate Desc";
		role = "%" + role + "%";
		return findPage(page, queryString, role);
	}

	@Override
	public Page<User> listUserPage(Page<User> page, String by, String condition) {

		String queryString = "FROM User u WHERE u." + by + " like '%"+condition+"%' ORDER BY u.lastLoginDate Desc";
		return findPage(page, queryString);
	}

	@Override
	public User getUser(Integer id) {
		String queryString="FROM User u WHERE u.id=? ";
		return findOne(queryString, null, id);
	}

	@Override
	public Page<User> findUserPage(Page<User> page) {
		String queryString = "FROM User ORDER BY lastLoginDate Desc";
		return findPage(page, queryString);		
	}

	@Override
	public Page<User> findUserByName(Page<User> page, String name) {
		String queryString = "FROM User WHERE name = ? ORDER BY lastLoginDate Desc";
		return findPage(page, queryString, name);
	}

}
